home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / CHAP02.TXT < prev    next >
Text File  |  1989-12-30  |  16KB  |  323 lines

  1.                       Chapter 2 - Getting started in C
  2.  
  3.  
  4.                             YOUR FIRST C PROGRAM
  5.  
  6.              The  best way to get started with C is to actually look 
  7.         at  a program,  so load the file named TRIVIAL.C  into  your 
  8.         editor  and display it on the monitor.   You are looking  at 
  9.         the  simplest  possible  C  program.   There is  no  way  to 
  10.         simplify   this   program   or  to   leave   anything   out.  
  11.         Unfortunately, the program doesn't do anything.
  12.  
  13.              The  word  "main" is very important,  and  must  appear 
  14.         once,  and only once in every C program.   This is the point 
  15.         where execution is begun when the program is run.   We  will 
  16.         see  later that this does not have to be the first statement 
  17.         in  the  program  but  it must exist  as  the  entry  point.  
  18.         Following  the "main" program name is a pair of  parentheses 
  19.         which  are  an  indication to the compiler that  this  is  a 
  20.         function.   We will cover exactly what a function is in  due 
  21.         time.   For now,  I suggest that you simply include the pair 
  22.         of parentheses. 
  23.  
  24.              The  two curly brackets,  properly called  braces,  are 
  25.         used to define the limits of the program itself.  The actual 
  26.         program  statements  go between the two braces and  in  this 
  27.         case,  there  are  no  statements because the  program  does 
  28.         absolutely nothing.   You can compile and run this  program, 
  29.         but since it has no executable statements,  it does nothing.  
  30.         Keep in mind however, that it is a valid C program.
  31.  
  32.                        A PROGRAM THAT DOES SOMETHING
  33.  
  34.              For  a much more interesting program,  load the program 
  35.         named WRTSOME.C and display it on your monitor.   It is  the 
  36.         same  as  the  previous  program  except  that  it  has  one 
  37.         executable statement between the braces.
  38.  
  39.              The  executable  statement is another  function.   Once 
  40.         again,  we will not worry about what a function is, but only 
  41.         how  to  use  this one.   In order to  output  text  to  the 
  42.         monitor,  it  is  put  within the function  parentheses  and 
  43.         bounded by quotation marks.  The end result is that whatever 
  44.         is included between the quotation marks will be displayed on 
  45.         the monitor when the program is run. 
  46.  
  47.              Notice the semi-colon at the end of the line.  C uses a 
  48.         semi-colon as a statement terminator,  so the semi-colon  is 
  49.         required  as  a  signal to the compiler that  this  line  is 
  50.         complete.   This  program  is also executable,  so  you  can 
  51.         compile  and  run  it to see if it does what  you  think  it 
  52.         should.
  53.  
  54.  
  55.  
  56.  
  57.                                  Page 7
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                       Chapter 2 - Getting started in C
  68.  
  69.  
  70.                       ANOTHER PROGRAM WITH MORE OUTPUT
  71.  
  72.              Load  the  program  WRTMORE.C and display  it  on  your 
  73.         monitor  for an example of more output and another small but 
  74.         important concept.  You will see that there are four program 
  75.         statements  in  this  program,  each one  being  a  "printf" 
  76.         function  statement.   The top line will be executed  first, 
  77.         then the next, and so on, until the fourth line is complete.  
  78.         The statements are executed in order from top to bottom.
  79.  
  80.              Notice  the funny character near the end of  the  first 
  81.         line,  namely  the backslash.   The backslash is used in the 
  82.         printf statement to indicate a special control character  is 
  83.         following.  In this case, the "n" indicates that a "newline" 
  84.         is requested.  This is an indication to return the cursor to 
  85.         the left side of the monitor and move down one line.   It is 
  86.         commonly  referred to as a carriage return/line  feed.   Any 
  87.         place  within  text that you desire,  you can put a  newline 
  88.         character  and start a new line.   You could even put it  in 
  89.         the  middle of a word and split the word between two  lines.  
  90.         The  C compiler considers the combination of  the  backslash 
  91.         and letter n as one character.
  92.  
  93.              A complete description of this program is now possible.  
  94.         The  first  printf  outputs a line of text and  returns  the 
  95.         carriage.   The  second printf outputs a line but  does  not 
  96.         return the carriage so the third line is appended to that of 
  97.         the second, then followed by two carriage returns, resulting 
  98.         in  a blank line.   Finally the fourth printf outputs a line 
  99.         followed by a carriage return and the program is complete.
  100.  
  101.              Compile and run this program to see if it does what you 
  102.         expect  it to do.   It would be a good idea at this time for 
  103.         you to experiment by adding additional lines of printout  to 
  104.         see if you understand how the statements really work.
  105.  
  106.                           LETS PRINT SOME NUMBERS
  107.  
  108.              Load  the  file  named ONEINT.C and display it  on  the 
  109.         monitor for our first example of how to work with data in  a 
  110.         C program.  The entry point "main" should be clear to you by 
  111.         now as well as the beginning brace.   The first new thing we 
  112.         encounter is the line containing "int index;", which is used 
  113.         to define an integer variable named "index".  The "int" is a 
  114.         reserved  word  in  C,  and can therefore not  be  used  for 
  115.         anything else.   It defines a variable that can have a value 
  116.         from  -32768 to 32767 on most microcomputer  implementations 
  117.         of  C.   Consult your users manual for the exact  definition 
  118.         for your compiler.   The variable name,  "index", can be any 
  119.         name that follows the rules for an identifier and is not one 
  120.         of  the reserved words for C.   Consult your manual  for  an 
  121.  
  122.  
  123.                                  Page 8
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                       Chapter 2 - Getting started in C
  134.  
  135.  
  136.         exact  definition of an identifier for your  compiler.   The 
  137.         final  character  on  the  line,   the  semi-colon,  is  the 
  138.         statement terminator used in C.
  139.  
  140.              We will see in a later chapter that additional integers 
  141.         could  also  be defined on the same line,  but we  will  not 
  142.         complicate the present situation. 
  143.  
  144.              Observing the main body of the program, you will notice 
  145.         that  there are three statements that assign a value to  the 
  146.         variable  "index",  but only one at a time.   The first  one 
  147.         assigns the value of 13 to "index", and its value is printed 
  148.         out.   (We will see how shortly.)  Later, the value of 27 is 
  149.         assigned to "index",  and finally 10 is assigned to it, each 
  150.         value  being  printed out.   It should be intuitively  clear 
  151.         that  "index"  is  indeed  a variable  and  can  store  many 
  152.         different  values.   Please note that many times  the  words 
  153.         "printed  out" are used to mean "displayed on the  monitor".  
  154.         You  will  find that in many cases  experienced  programmers 
  155.         take  this  liberty,  probably due to the "printf"  function 
  156.         being used for monitor display.
  157.  
  158.                           HOW DO WE PRINT NUMBERS
  159.  
  160.              To  keep  our promise,  let's return  to  the  "printf" 
  161.         statements  for a definition of how they work.   Notice that 
  162.         they are all identical and that they all begin just like the 
  163.         "printf"  statements  we  have  seen  before.    The   first 
  164.         difference occurs when we come to the % character.   This is 
  165.         a  special character that signals the output routine to stop 
  166.         copying characters to the output and do something different, 
  167.         namely output a variable.   The % sign is used to signal the 
  168.         start  of  many different types of variables,  but  we  will 
  169.         restrict  ourselves  to  only one  for  this  example.   The 
  170.         character following the % sign is a "d",  which signals  the 
  171.         output routine to get a decimal value and output it.   Where 
  172.         the decimal value comes from will be covered shortly.  After 
  173.         the  "d",  we  find the familiar \n,  which is a  signal  to 
  174.         return the video "carriage", and the closing quotation mark.
  175.  
  176.              All  of  the  characters between  the  quotation  marks 
  177.         define  the pattern of data to be output by this  statement, 
  178.         and  after  the pattern,  there is a comma followed  by  the 
  179.         variable name "index".  This is where the "printf" statement 
  180.         gets  the decimal value which it will output because of  the 
  181.         "%d"  we saw earlier.   We could add more "%d" output  field 
  182.         descriptors within the brackets and more variables following 
  183.         the  description  to cause more data to be printed with  one 
  184.         statement.   Keep in mind however, that it is important that 
  185.         the  number of field descriptors and the number of  variable 
  186.  
  187.  
  188.  
  189.                                  Page 9
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                       Chapter 2 - Getting started in C
  200.  
  201.  
  202.         definitions must be the same or the runtime system will  get 
  203.         confused and probably quit with a runtime error.
  204.  
  205.              Much  more  will  be  covered at a later  time  on  all 
  206.         aspects of input and output formatting.   A reasonably  good 
  207.         grasp  of this topic is necessary in order to understand the 
  208.         following  lessons.   It  is  not  necessary  to  understand 
  209.         everything about output formatting at this time, only a fair 
  210.         understanding of the basics.
  211.  
  212.              Compile and run ONEINT.C and observe the output.
  213.  
  214.                         HOW DO WE ADD COMMENTS IN C
  215.  
  216.              Load the file COMMENTS.C and observe it on your monitor 
  217.         for an example of how comments can be added to a C  program.  
  218.         Comments  are  added to make a program more readable to  you 
  219.         but the compiler must ignore the comments.   The slash  star 
  220.         combination  is used in C for comment delimiters.   They are 
  221.         illustrated  in the program at hand.   Please note that  the 
  222.         program does not illustrate good commenting practice, but is 
  223.         intended  to illustrate where comments can go in a  program.  
  224.         It is a very sloppy looking program.
  225.  
  226.              The  first slash star combination introduces the  first 
  227.         comment  and  the star slash at the end of  the  first  line 
  228.         terminates this comment.  Note that this comment is prior to 
  229.         the beginning of the program illustrating that a comment can 
  230.         precede the program itself.  Good programming practice would 
  231.         include  a  comment  prior  to  the  program  with  a  short 
  232.         introductory  description of the program.   The next comment 
  233.         is  after the "main()" program entry point and prior to  the 
  234.         opening brace for the program code itself.
  235.  
  236.              The  third  comment starts after the  first  executable 
  237.         statement and continues for four lines.   This is  perfectly 
  238.         legal  because  a comment can continue for as many lines  as 
  239.         desired  until  it is terminated.   Note carefully  that  if 
  240.         anything  were included in the blank spaces to the  left  of 
  241.         the  three  continuation lines of the comment,  it would  be 
  242.         part  of the comment and would not be  compiled.   The  last 
  243.         comment  is located following the completion of the program, 
  244.         illustrating  that  comments can go nearly anywhere in  a  C 
  245.         program. 
  246.  
  247.              Experiment  with  this program by  adding  comments  in 
  248.         other places to see what will happen. Comment out one of the 
  249.         printf  statements by putting comment delimiters both before 
  250.         and after it and see that it does not get printed out.
  251.  
  252.  
  253.  
  254.  
  255.                                  Page 10
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                       Chapter 2 - Getting started in C
  266.  
  267.  
  268.              Comments are very important in any programming language 
  269.         because  you will soon forget what you did and why  you  did 
  270.         it.   It  will  be  much  easier to modify  or  fix  a  well 
  271.         commented  program  a year from now than one with few or  no 
  272.         comments.   You will very quickly develop your own  personal 
  273.         style of commenting.
  274.  
  275.              Some  compilers allow you to "nest" comments which  can 
  276.         be very handy if you need to "comment out" a section of code 
  277.         during debugging.  Check your compiler documentation for the 
  278.         availability  of this feature with you particular  compiler.  
  279.         Compile and run COMMENTS.C at this time.
  280.  
  281.                            GOOD FORMATTING STYLE
  282.  
  283.              Load  the  file  GOODFORM.C  and  observe  it  on  your 
  284.         monitor.   It  is  an example of a well  formatted  program.  
  285.         Even though it is very short and therefore does very little, 
  286.         it  is very easy to see at a glance what it does.   With the 
  287.         experience  you have already gained in  this  tutorial,  you 
  288.         should  be  able  to very quickly grasp the meaning  of  the 
  289.         program in it's entirety.  Your C compiler ignores all extra 
  290.         spaces  and  all carriage returns  giving  you  considerable 
  291.         freedom  concerning how you format your program.   Indenting 
  292.         and  adding spaces is entirely up to you and is a matter  of 
  293.         personal  taste.   Compile and run the program to see if  it 
  294.         does what you expect it to do.
  295.  
  296.              Now load and display the program UGLYFORM.C and observe 
  297.         it.   How  long  will it take you to figure  out  what  this 
  298.         program  will do?   It doesn't matter to the compiler  which 
  299.         format style you use, but it will matter to you when you try 
  300.         to  debug  your program.   Compile this program and run  it.  
  301.         You may be surprised to find that it is the same program  as 
  302.         the  last  one,  except for the formatting.   Don't get  too 
  303.         worried about formatting style yet.  You will have plenty of 
  304.         time  to  develop  a  style of your own  as  you  learn  the 
  305.         language.   Be observant of styles as you see C programs  in 
  306.         magazines, books, and other publications. 
  307.  
  308.              This  should  pretty well cover the basic  concepts  of 
  309.         programming  in  C,  but as there are many other  things  to 
  310.         learn, we will forge ahead to additional program structure.
  311.  
  312.         PROGRAMMING EXERCISES
  313.  
  314.         1. Write a program to display your name on the monitor.
  315.  
  316.         2. Modify  the  program to display your address  and  phone 
  317.            number  on  separate  lines  by  adding  two  additional 
  318.            "printf" statements.
  319.  
  320.  
  321.                                  Page 11
  322.  
  323.